package com.elitel.frame.base.controller.polym;


import com.elitel.frame.base.service.PolymService;
import com.elitel.frame.main.entity.BaseQueryfield;
import com.elitel.frame.main.entity.BaseShowfield;
import com.elitel.frame.main.entity.ext.BaseShowfieldExt;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.polym.CfgMultQueryField;
import com.elitel.frame.main.entity.polym.CfgMultResultField;
import com.elitel.frame.main.entity.polym.PolymVO;
import com.elitel.frame.main.entity.polym.SaveParamsInfoDTO;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("system/polym")
public class PolymController {
    Logger logger = LoggerFactory.getLogger(PolymController.class);
    @Autowired
    private PolymService polymService;


    /**
     * 获取聚合服务配置信息
     */
    @PostMapping("/getPolymInfo")
    public Object getPolymInfo(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> res = polymService.getPolymInfo(map);
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        return jsonResult;

    }


    /**
     * 保存聚合服务配置信息
     */
    @PostMapping("/saveCfgInfo")
    public Object saveCfgInfo(@RequestBody PolymVO params) {
        JsonResult jsonResult = new JsonResult();
        try {
            polymService.saveCfgInfo(params);
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            logger.error("保存异常", e);
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;

    }

    /**
     * 查询服务列表
     */
    @PostMapping("/serverList")
    public JsonResult serverList(@RequestBody Map<String, Object> params) {
        JsonResult jsonResult = new JsonResult();
        List<PrjServiceExt> list = polymService.getServerList(params);
        jsonResult.setSuccess(true);
        jsonResult.setData(list);
        return jsonResult;

    }

    /**
     * 根据服务id查询参数字段
     */
    @PostMapping("/getParamsById")
    public JsonResult getParamsById(@RequestBody Map<String, Object> params) {
        JsonResult jsonResult = new JsonResult();
        List<BaseQueryfield> baseQueryfieldExts = polymService.getParamsById(params);
        jsonResult.setData(baseQueryfieldExts);
        jsonResult.setSuccess(true);
        return jsonResult;

    }
    /**
     * 根据服务id查询参数字段
     */
    @PostMapping("/getResById")
    public JsonResult getResById(@RequestBody Map<String, Object> params) {
        JsonResult jsonResult = new JsonResult();
        List<BaseShowfieldExt> resById = polymService.getResById(params);
        jsonResult.setData(resById);
        jsonResult.setSuccess(true);
        return jsonResult;

    }

    /**
     * 获取聚合服务配置信息
     */
    @PostMapping("/saveParamsInfo")
    public Object saveParamsInfo(@RequestBody SaveParamsInfoDTO dto) {
        JsonResult jsonResult = new JsonResult();
        try {
            polymService.saveParamsInfo(dto);
            jsonResult.setSuccess(true);
            return jsonResult;
        } catch (Exception e) {
            logger.error("保存异常", e);
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }


    /**
     * 获取聚合服务配置信息
     */
    @PostMapping("/saveResInfo")
    public Object saveResInfo(@RequestBody SaveResInfoDTO dto) {
        JsonResult jsonResult = new JsonResult();
        try {
            polymService.saveResInfo(dto.list, dto.serviceId);
            jsonResult.setSuccess(true);
            return jsonResult;
        } catch (Exception e) {
            logger.error("保存异常", e);
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }


    /**
     * 获取聚合服务配置信息
     */
    @PostMapping("/getParamsInfo")
    public Object getParamsInfo(@RequestBody Map<String, Object> params) {
        JsonResult jsonResult = new JsonResult();
        List<CfgMultQueryField> res = polymService.getParamsInfo(params);
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        return jsonResult;

    }

    /**
     * 获取聚合服务配置信息
     */
    @PostMapping("/getResInfo")
    public Object getResInfo(@RequestBody Map<String, Object> params) {
        JsonResult jsonResult = new JsonResult();
        List<CfgMultResultField> res = polymService.getResInfo(params);
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        return jsonResult;

    }



    @Data
    public static class SaveResInfoDTO implements Serializable {
        private String serviceId;
        private List<CfgMultResultField> list;
    }


}
